from 数据库 import de_login, y_query, g_query
from PyQt5.QtWidgets import QApplication, QMessageBox, QWidget
import redis
import pymysql
import sys


r_y = redis.StrictRedis(host='localhost', port=6379, db=0)
r_g = redis.StrictRedis(host='localhost', port=6379, db=1)

class MainDialog(QWidget):
    def __init__(self, parent=None):
        super(QWidget, self).__init__(parent)
        self.ui = de_login.Ui_login()
        self.ui.setupUi(self)
        self.chile_Win = y_query.MainDialog()
        self.chile_Win2 = g_query.MainDialog()

    def login_y(self):
        name = self.ui.lineEdit.text()
        password = self.ui.lineEdit_2.text()
        if name:
            if password:
                if r_y.exists(name):
                    if r_y.get(name).decode() == password:
                        QMessageBox.about(self, ' ', '登录成功')
                        self.hide()
                        self.chile_Win.append(name)
                        self.chile_Win.exec_()
                    else:
                        QMessageBox.about(self, ' ', '密码错误')
                else:
                    QMessageBox.about(self, ' ', '查无此用户')
            else:
                QMessageBox.about(self, ' ', '密码为空')
        else:
            QMessageBox.about(self, ' ', '账号为空')

    def login_g(self):
        name = self.ui.lineEdit.text()
        password = self.ui.lineEdit_2.text()
        if name:
            if password:
                if r_g.exists(name):
                    if r_g.get(name).decode() == password:
                        QMessageBox.about(self, ' ', '登录成功')
                        self.hide()
                        self.chile_Win2.append(name)
                        self.chile_Win2.exec_()
                    else:
                        QMessageBox.about(self, ' ', '密码错误')
                else:
                    QMessageBox.about(self, ' ', '查无此用户')
            else:
                QMessageBox.about(self, ' ', '密码为空')
        else:
            QMessageBox.about(self, ' ', '账号为空')

    def registered(self):
        name = self.ui.lineEdit.text()
        password = self.ui.lineEdit_2.text()
        if name:
            if password and r_y.exists(name):
                QMessageBox.about(self, ' ', '该用户已经存在了哦')
            else:
                if password:
                    r_y.set(name, password)
                    QMessageBox.about(self, ' ', '注册成功')
                    conn = pymysql.connect(host='127.0.0.1', user='root', passwd='dqzx2020', port=3306, db='design', charset='utf8')
                    cur = conn.cursor()
                    sql = "select * from 员工"
                    cur.execute(sql)
                    data = cur.fetchall()
                    sql = "insert into 员工(员工编号,员工姓名,员工密码) values ({},'{}','{}')".format(len(data)+1, name, password)
                    cur.execute(sql)
                    conn.commit()
                    conn.close()
                else:
                    QMessageBox.about(self, ' ', '密码为空')
        else:
            QMessageBox.about(self, ' ', '账号为空')

    def aa(self):
        print(123)


if __name__ == '__main__':
    myapp = QApplication(sys.argv)
    myDlg = MainDialog()
    myDlg.show()
    sys.exit(myapp.exec_())